/*
 * LoopMediaReader.h
 *
 *  Created on: 2021年5月7日
 *      Author: chuanjiang.zh
 */

#ifndef LOOPMEDIAREADER_H_
#define LOOPMEDIAREADER_H_

#include "UMediaReader.h"
#include <string>
#include <atomic>
#include <mutex>
#include "TEvent.h"


class LoopMediaReader : public UMediaReader
{
public:
    LoopMediaReader(UMediaReader* reader, bool owned, int count = -1);
    virtual ~LoopMediaReader();

	int getCount();

	void setCount(int count);

	void setRetryInterval(int ms);


	virtual int open(const char* url, const char* params);

	virtual void close();

	virtual bool isOpen();

	virtual bool getFormat(UMediaFormat& fmt);

	virtual int getDuration();

	virtual bool play();

	virtual bool pause();

	virtual void stop();

	virtual int getState();

	virtual bool seekable();

	virtual bool seek(int64_t offset);

	virtual int64_t getTime();

	virtual bool setScale(float scale);

	virtual float getScale();

	virtual int read(UMediaPacket& pkt);

	virtual void interrupt();

	virtual bool isLive();


	virtual const char* getFormatName();

	virtual size_t getMetaData(const char* name, char* value, size_t size);


protected:
	void setState(UMediaState state);


protected:
	UMediaReader*	m_reader;
	bool	m_owned;
	//std::atomic_int	m_count;
	int		m_count;
	std::atomic_bool	m_opened;

	std::string	m_url;
	std::string	m_params;

	UMediaFormat	m_format;
	UMediaState		m_state;

	std::mutex	m_mutex;
	int		m_curCount;
	int64_t	m_tsOffset;
	
	int		m_retryInterval;
	comn::Event	m_eventDelay;

};

#endif /* LOOPMEDIAREADER_H_ */
